<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="117px"
    >
      <el-form-item label="运动名称" prop="sportName">
        <el-input
          v-model="queryParams.sportName"
          placeholder="请输入运动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运动会主题" prop="sportTheme">
        <el-input
          v-model="queryParams.sportTheme"
          placeholder="请输入运动会主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运动会举办地点" prop="sportAddress">
        <el-input
          v-model="queryParams.sportAddress"
          placeholder="请输入运动会举办地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="运动会开幕时间" prop="sportStart">
        <el-date-picker
          clearable
          v-model="queryParams.sportStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择运动会开幕时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="运动会闭幕时间" prop="sportEnd">
        <el-date-picker
          clearable
          v-model="queryParams.sportEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择运动会闭幕时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sport:information:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sport:information:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sport:information:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sport:information:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="informationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="运动会id" align="center" prop="sportId" ></el-table-column> -->
      <el-table-column label="运动名称" align="center" prop="sportName" />
      <el-table-column label="运动会主题" align="center" prop="sportTheme" />
      <el-table-column
        label="运动会举办地点"
        align="center"
        prop="sportAddress"
      />
      <el-table-column
        label="运动会状态"
        align="center"
        prop="sportStatus"
        sortable
      >
        <template slot-scope="scope">
          <span v-if="scope.row.sportStatus == 1">
            <el-button type="danger" :loading="true" round>开幕</el-button>
          </span>
          <span v-if="scope.row.sportStatus == 0">
            <el-button type="primary" round>闭幕</el-button>
          </span>
          <span v-if="scope.row.sportStatus == 2">
            <el-button type="primary" round>待开幕</el-button>
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="运动会开幕时间"
        align="center"
        prop="sportStart"
        width="180"
        sortable
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sportStart, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="运动会闭幕时间"
        align="center"
        prop="sportEnd"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sportEnd, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!--   <span v-if="scope.row.sportStatus == 0" >
              <el-button type="primary"  round>闭幕</el-button>
            </span> -->

          <!-- 1是开幕 -->
          <span v-if="scope.row.sportStatus == 1">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-claim"
              @click="addxm(scope.row)"
              >查询项目&nbsp;</el-button
            >
          </span>
          <!-- 其余的运动会状态就不显示 -->
          <span v-else-if="scope.row.sportStatus == 2"> </span>
          <span v-else-if="scope.row.sportStatus == 0"></span>

          &nbsp;&nbsp;&nbsp;
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sport:information:edit']"
            >&nbsp; &nbsp;修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sport:information:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运动会届对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="119px">
        <el-form-item label="运动名称" prop="sportName">
          <el-input v-model="form.sportName" placeholder="请输入运动名称" />
        </el-form-item>
        <el-form-item label="运动会主题" prop="sportTheme">
          <el-input v-model="form.sportTheme" placeholder="请输入运动会主题" />
        </el-form-item>
        <el-form-item label="运动会举办地点" prop="sportAddress">
          <el-input
            v-model="form.sportAddress"
            placeholder="请输入运动会举办地点"
          />
        </el-form-item>

        <el-form-item label="运动会状态" prop="sportStatus">
          <el-select v-model="form.sportStatus" placeholder="请选择输出情况">
            <el-option label="开幕" value="1" />
            <el-option label="闭幕" value="0" />
            <el-option label="待开幕" value="2" />
          </el-select>
        </el-form-item>

        <el-form-item label="运动会开幕时间" prop="sportStart">
          <el-date-picker
            clearable
            v-model="form.sportStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择运动会开幕时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="运动会闭幕时间" prop="sportEnd">
          <el-date-picker
            clearable
            v-model="form.sportEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择运动会闭幕时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInformation,
  getInformation,
  delInformation,
  addInformation,
  updateInformation,
} from "@/api/sport/information";

export default {
  name: "Information",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运动会届表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sportName: null,
        sportTheme: null,
        sportAddress: null,
        sportStatus: "",
        sportStart: null,
        sportEnd: null,
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 1、增加运动会项目具体信息 */
    addxm(row) {
      //跳转页面进入---增加项目具体信息  携带运动会id
      //  console.log(row)
      this.$router.push({ name: "Sportlist", params: { rowlist: row } }); // 只能用 name
    },

    /** 查询运动会届列表 */
    getList() {
      this.loading = true;
      listInformation(this.queryParams).then((response) => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sportId: null,
        sportName: null,
        sportTheme: null,
        sportAddress: null,
        sportStatus: null,
        sportStart: null,
        sportEnd: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.sportId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运动会届";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sportId = row.sportId || this.ids;
      getInformation(sportId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运动会届";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.sportId != null) {
            updateInformation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInformation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sportIds = row.sportId || this.ids;
      this.$modal
        .confirm('是否确认删除运动会届编号为"' + sportIds + '"的数据项？')
        .then(function () {
          return delInformation(sportIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sport/information/export",
        {
          ...this.queryParams,
        },
        `information_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
